/*
Copyright 2010-2011 CumulusCloud
http://cumuluscloud.cc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package appmanager.client.views;

import java.util.List;

import appmanager.client.AppService;
import appmanager.client.AppServiceAsync;
import appmanager.shared.vo.RemoteApp;

import com.google.gwt.cell.client.ActionCell;
import com.google.gwt.cell.client.ActionCell.Delegate;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

public class UpdateApplicationsView extends Composite {
	interface UpdateApplicationsViewUiBinder extends	UiBinder<Widget, UpdateApplicationsView> {}
	private static UpdateApplicationsViewUiBinder uiBinder = GWT.create(UpdateApplicationsViewUiBinder.class);
	private final AppServiceAsync appService = GWT.create(AppService.class);
	
	@UiField CellTable<RemoteApp> table;
	
	// For the list
	private AsyncCallback<List<RemoteApp>> retrievedLocalUpdateApps = new AsyncCallback<List<RemoteApp>>() {
		@Override
		public void onFailure(Throwable caught) {
			Window.alert("Could not connect to server");
		}
		
		@Override
		public void onSuccess(List<RemoteApp> result) {
			table.setRowCount(result.size());
			table.setRowData(0, result);
		}
	};
	
	// For updating
	@SuppressWarnings("rawtypes")
	private AsyncCallback installCallback = new AsyncCallback() {
		@Override
		public void onFailure(Throwable caught) {
			Window.alert("Error requesting installation");
		}

		@Override
		public void onSuccess(Object result) {}
	};

	private Delegate<RemoteApp> updateDelegate = new Delegate<RemoteApp>() {
		@Override
		public void execute(RemoteApp object) {
			appService.install(object, installCallback);
		}
	};
	
	private Cell<RemoteApp> updateCell = new ActionCell<RemoteApp>("update", updateDelegate );

	// Main
	public UpdateApplicationsView() {
		initWidget(uiBinder.createAndBindUi(this));
		
		// The columns
		TextColumn<RemoteApp> webnameColumn = new TextColumn<RemoteApp>() {
			@Override
			public String getValue(RemoteApp object) {
				return object.getWebName();
			}
		};
		Column<RemoteApp, RemoteApp> updateColumn = new Column<RemoteApp, RemoteApp>(updateCell ) {
			@Override
			public RemoteApp getValue(RemoteApp object) {
				return object;
			}
		};
		
		// Add the columns
		table.addColumn(webnameColumn, "Webname");
		table.addColumn(updateColumn);
		
		// Retrieve the list
		appService.getLocalUpdateList(retrievedLocalUpdateApps);
	}
}
